using System;
using UnityEngine;

namespace Pumpkin.Utils
{
    public static class LogColor
    {
        //#0000ffff
        public const string Blue = "blue";
        //#ffff00ff
        public const string Yellow = "yellow";
        //#000000ff
        public const string Black = "black";
        //#ff0000ff
        public const string Red = "red";
        //#00ffffff
        public const string Aqua = "aqua";
        //#a52a2aff
        public const string Brown = "brown";
        //#0000a0ff
        public const string DarkBlue = "darkblue";
        //#00ffffff
        public const string Cyan = "cyan";
        //#ff00ffff
        public const string Fuchsia = "fuchsia";
        //#008000ff
        public const string Green = "green";
        //#808080ff
        public const string Grey = "grey";
        //#add8e6ff
        public const string LightBlue = "lightblue";
        //#00ff00ff
        public const string Lime = "lime";
        //#ff00ffff
        public const string Magenta = "magenta";
        //#800000ff
        public const string Maroon = "maroon";
        //#000080ff
        public const string Navy = "navy";
        //#808000ff
        public const string Olive = "olive";
        //#ffa500ff
        public const string Orange = "orange";
        //#800080ff
        public const string Purple = "purple";
        //#c0c0c0ff
        public const string Silver = "silver";
        //#008080ff
        public const string Teal = "teal";
        //#ffffffff
        public const string White = "white";

        public static string ToHtmlString(string colorName)
        {
            switch(colorName)
            {
                case Blue:
                    return "0000ffff";
                case Yellow:
                    return "ffff00ff";
                case Black:
                    return "000000ff";
                case Red:
                    return "ff0000ff";
                case Aqua:
                    return "00ffffff";
                case Brown:
                    return "a52a2aff";
                case DarkBlue:
                    return "0000a0ff";
                case Cyan:
                    return "00ffffff";
                case Fuchsia:
                    return "ff00ffff";
                case Green:
                    return "008000ff";
                case Grey:
                    return "808080ff";
                case LightBlue:
                    return "add8e6ff";
                case Lime:
                    return "00ff00ff";
                case Magenta:
                    return "ff00ffff";
                case Maroon:
                    return "800000ff";
                case Navy:
                    return "000080ff";
                case Olive:
                    return "808000ff";
                case Orange:
                    return "ffa500ff";
                case Purple:
                    return "800080ff";
                case Silver:
                    return "c0c0c0ff";
                case Teal:
                    return "008080ff";
                case White:
                    return "ffffffff";
                default:
                    return colorName;
            }
        }

        public static Color ToColor(string htmlString)
        {
            Color color;
            if(ColorUtility.TryParseHtmlString(htmlString, out color))
            {
                return color;
            }
            else
            {
                string msg = string.Format("HTML color:{0} cant convert to Unity Color", htmlString);
                throw new InvalidOperationException(msg);
            }
        }
    }

    public interface ILog
    {
        void LogInfo(string msg, string color);
        void LogDebug(string msg, string color);
        void LogWarning(string msg, string color);
        void LogError(string msg, string color);
    }

    public static class Log
    {
        public static bool DisableLog = false;
        private static ILog logger_;
        private static ILog visualLogger_;
        private static bool enableVisual = false;
        public static void SetLogger(ILog logger)
        {
            logger_ = logger;
        }

        public static void SetVisualLogger(ILog logger)
        {
            visualLogger_ = logger;
        }

        public static void EnableVisualLog()
        {
            if(visualLogger_ != null)
            {
                throw new InvalidOperationException("VisualLogger was not set");
            }
            enableVisual = true;
        }

        public static void DisableVisualLog()
        {
            enableVisual = false;
        }

        public static void LogInfo(string msg, string color = LogColor.Black)
        {
            if(DisableLog) return;
            logger_.LogInfo(msg, color);
            if (enableVisual) visualLogger_.LogInfo(msg, color);
        }

        public static void LogDebug(string msg, string color = LogColor.Black)
        {
            if(DisableLog) return;
            logger_.LogDebug(msg, color);
            if (enableVisual) visualLogger_.LogDebug(msg, color);
        }

        public static void LogWarning(string msg, string color = LogColor.Yellow)
        {
            if(DisableLog) return;
            logger_.LogWarning(msg, color);
            if (enableVisual) visualLogger_?.LogWarning(msg, color);
        }

        public static void LogError(string msg, string color = LogColor.Red)
        {
            if(DisableLog) return;
            logger_.LogError(msg, color);
            if (enableVisual) visualLogger_.LogError(msg, color);
        }
    }
}